﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DrugStorehouseMIS.BLL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.QueryAndReport;

namespace DrugStorehouseMIS.StockInAndOutManager
{
    public partial class frmStockInDetailNew : Form
    {
        private StockInDetail stockInDetailBLL;
        private Drug drugBLL;

        public frmStockInDetailNew()
        {
            InitializeComponent();

            stockInDetailBLL = new StockInDetail();
            drugBLL = new Drug();
            stockInDetailBLL.DrugNum = new Drug();          
        }

        private void frmStockInDetailNew_Load(object sender, EventArgs e)
        {
           // BindMeasuringUnit();
        }

        public void Init(StockInHeader header)
        {
            txtStockInNum.Text = header.StockInNum;
            txtStockInType.Text = header.StockInTypeName.StockInTypeName;
            txtStockInDatetime.Text = header.StockInDatetime.ToString();
            txtSupplier.Text = header.SupplierName.SupplierName;
        }
   
         
        /// <summary>
        /// 根据输入的数量和进价来计算金额
         /// </summary>
        public void Calcluate()
        {
            float price, amount;
            long qty;

            if (txtImportPrice.Text.Trim() != "")
            {
                price = Convert.ToSingle(txtImportPrice.Text.Trim());
            }
            else
            {
                price = 0;
            }
            if (txtQuantity.Text.Trim() != "")
            {
                qty = Convert.ToInt64(txtQuantity.Text.Trim());
            }
            else
            {
                qty = 0;
            }
            amount = price * qty;
            txtAmount.Text = Convert.ToString(amount);
        }
 
        public bool Check()
        {
            if (txtAmount.Text.Trim() != "" && txtBatchNum.Text.Trim() != "" && txtDrugNum.Text.Trim() != "" && txtImportPrice.Text.Trim() != "" && txtProductArea.Text.Trim() != "" && txtQuantity.Text.Trim() != "" )
                return btnOK.Enabled = true;
            else
                return btnOK.Enabled = false;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            stockInDetailBLL.CreateStockInDetail(GetObject());
            this.DialogResult = DialogResult.OK;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Tool.ResetAllTextBox(gbxDetailInfo);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtQuantity, '0');   //让文本框只能输入数字
            this.Check();
            this.Calcluate();         
        }

        private void txtImportPrice_TextChanged(object sender, EventArgs e)
        {
            Tool.OnlyNumber(txtImportPrice, '.');   
            this.Check();
            this.Calcluate();
        }

        /// <summary>
        /// 获取StockInDetail对象
        /// </summary>
        /// <returns></returns>
        private StockInDetail GetObject()
        {
            StockInDetail detail = new StockInDetail();
            detail.DrugNum = new Drug();

            detail.Amount = Convert.ToSingle(txtAmount.Text.Trim());
            detail.BatchNum = txtBatchNum.Text.Trim();
            detail.DrugNum.DrugNum = Convert.ToInt32(txtDrugNum.Text.Trim());
            detail.ExpiryDatetime = dtpExpiryDate.Value.Date;
            detail.ImportPrice = Convert.ToSingle(txtImportPrice.Text.Trim());       
            detail.ProductedArea = txtProductArea.Text.Trim();
            detail.ProductedDatetime = dtpProductedDate.Value.Date;
            detail.Quantity = Convert.ToInt32(txtQuantity.Text.Trim());
            detail.StockInNum = txtStockInNum.Text.Trim();

            return detail;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            frmDrugInfo form = new frmDrugInfo();
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                //设置各种信息
                txtDrugNum.Text = Convert.ToString(form.DrugBLL.DrugNum);
                txtDrugName.Text = form.DrugBLL.TradeName;
                txtSpecifications.Text = form.DrugBLL.Sepcifications;
                txtMeasuringUnit.Text = form.DrugBLL.MeasuringUnitNum.MeasuringUnitName;
                txtTradePrice.Text = Convert.ToString(form.DrugBLL.TradePrice);
                txtRetailPrice.Text = Convert.ToString(form.DrugBLL.RetailPrice);
            }
        }
    }
}